package com.heer.gymnasium.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.AppHelper;
import com.heer.gymnasium.R;
import com.heer.gymnasium.util.ScreenUtils;
import com.heer.model.QrcodeAccess;
import com.zbar.lib.utils.QRImageUtils;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by heer on 17/5/15.
 */

public class QRAccessDialog extends Dialog {

    private ImageView img;
    private TextView tv;
    private ImageView imgClose;

    private String qrString;
    private Context mContext;

    public QRAccessDialog(Context context) {
        super(context);
        this.mContext = context;
        init(context);
    }

    public QRAccessDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        init(context);
    }

    protected QRAccessDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
        init(context);
    }

    public QRAccessDialog(Context context, String qr) {
        super(context, R.style.dialog);
        this.qrString = qr;
        this.mContext = context;
        init(context);
    }

    private void init(final Context context) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); //before
        this.setCanceledOnTouchOutside(true);
        setContentView(R.layout.dialog_qr_access);

        /*
         * 将对话框的大小按屏幕大小的百分比设置
		 */
        Window dialogWindow = this.getWindow();
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (ScreenUtils.getScreenW() * 0.65); // 宽度设置为屏幕的0.65
        p.height = (int) (ScreenUtils.getScreenW() * 0.65); // 宽度设置为屏幕的0.65
        dialogWindow.setAttributes(p);

        img = (ImageView) findViewById(R.id.image_scan);
        imgClose = (ImageView) findViewById(R.id.btn_close);
        tv = (TextView) findViewById(R.id.tv_desc);
        imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        AppHelper.context().apiService().getAccessQrcode(qrString).enqueue(new BaseCallback<ApiResponse<QrcodeAccess>>() {
            @Override
            public void onResponse(Call<ApiResponse<QrcodeAccess>> call, Response<ApiResponse<QrcodeAccess>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful() && response.body().isSccessful())
                    img.setImageBitmap(new QRImageUtils(mContext).createQRImage(response.body().getData().getQRstr()));
            }

            @Override
            public void onFailure(Call<ApiResponse<QrcodeAccess>> call, Throwable t) {
                super.onFailure(call, t);
            }
        });

    }


}

